/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bd2;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.ListSelectionModel;

import java.util.ArrayList;

/**
 *
 * @author tolkjen
 */
public class KatTable extends JTable {
    public KatTable()
    {
        setModel(new KatTableModel());
        
        setFocusable(false);
        setShowHorizontalLines(true);
        setShowVerticalLines(true);
        setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        setFillsViewportHeight(true);
        getTableHeader().setReorderingAllowed(false);
        getTableHeader().setResizingAllowed(false);
    }
    
    public void addRow(Object[] row)
    {
        KatTableModel model = (KatTableModel) getModel();
        model.addRow(row);
        repaint();
    }

    public void removeRow(int index)
    {
        KatTableModel model = (KatTableModel) getModel();
        model.removeRow(index);
        repaint();
    }

    public void clear()
    {
        KatTableModel model = (KatTableModel) getModel();
        while (model.getRowCount() > 0)
            model.removeRow(0);
        repaint();
    }

    public Object[] getRow(int index)
    {
        KatTableModel model = (KatTableModel) getModel();
        return model.row(index);
    }
}

class KatTableModel extends AbstractTableModel
{
    private String[] columnNames = {"Id", "Nazwa"};
    private ArrayList<Object[]> data;

    public KatTableModel()
    {
        data = new ArrayList<Object[]>();
    }

    public void addRow(Object[] row)
    {
        data.add(row);
    }

    public void removeRow(int index)
    {
        data.remove(index);
    }

    public Object[] row(int index)
    {
        return data.get(index);
    }

    @Override
    public int getColumnCount()
    {
        return columnNames.length;
    }

    @Override
    public int getRowCount()
    {
        return data.size();
    }

    @Override
    public String getColumnName(int col)
    {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col)
    {
        return data.get(row)[col];
    }

    @Override
    public Class getColumnClass(int c)
    {
        return Object.class;
    }

    @Override
    public boolean isCellEditable(int row, int col)
    {
        return false;
    }

    @Override
    public void setValueAt(Object value, int row, int col)
    {
        data.get(row)[col] = value;
        fireTableCellUpdated(row, col);
    }
}
